package com.itheima.mm.wx.controller;

import com.itheima.framework.annotation.HmComponent;
import com.itheima.framework.annotation.HmRequestMapping;
import com.itheima.framework.annotation.HmSetter;
import com.itheima.mm.base.BaseController;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.WxMember;
import com.itheima.mm.wx.service.CategoryService;
import com.itheima.mm.wx.service.WxMemberService;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：wanghao
 * @date ：Created in 2020/11/4
 * @description ：题目分类控制器
 * @version: 1.0
 */
@HmComponent
@Slf4j
public class CategoryController extends BaseController {

    @HmSetter("wxMemberService")
    private WxMemberService wxMemberService;

    @HmSetter("categoryService")
    private CategoryService categoryService;

    /**
     * 获取题目分类列表
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @HmRequestMapping("/category/list")
    public void getCateGoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取请求数据
            Map<String, Object> mapData = parseJSON2Object(request, HashMap.class);
            log.debug("获取用户传过来的数据：mapData:{}", mapData);
            // 从header中获取openId，并放入客户端参数容器
            String openId = getHeaderAuthorization(request);
            mapData.put("openId", openId);
            // 根据openId获取会员，同时获取memberId,cityId,courseId
            WxMember wxMember = wxMemberService.findWxMemberByOpenId(openId);
            mapData.put("memberId", wxMember.getId());
            mapData.put("courseId", wxMember.getCourseId());
            mapData.put("cityId", wxMember.getCityId());
            log.debug("调用service mapData:{}", mapData);
            // 调用Service获取题目分类列表
            List<Map> mapList = categoryService.findCategoryList(mapData);
            // 返回JSON
            printResult(response, new Result(true, "获取题目分类列表成功", mapList));
        } catch (Exception e) {
            e.printStackTrace();
            printResult(response, new Result(false, "获取题目分类列表失败"));
        }
    }

    /**
     * 题目分类列表下题目列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @HmRequestMapping("/category/question/list")
    public void getCategoryQuestionList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取前端数据
            Map<String,Object> mapData = parseJSON2Object(request,HashMap.class);
            //根据openId获取会员信息
            String openId = getHeaderAuthorization(request);
            WxMember wxMember = wxMemberService.findWxMemberByOpenId(openId);
            // 把会员ID，学科ID，城市ID存入参数容器
            mapData.put("memberId",wxMember.getId());
            mapData.put("courseId",wxMember.getCourseId());
            mapData.put("cityId",wxMember.getCityId());
            log.debug("调用Service，获取分类基本信息及分类下的题目列表 mapData:{}",mapData);
            Map<String,Object> resultMap = categoryService.findCategoryQuestionList(mapData);
            // 返回JSON
            printResult(response,new Result(true,"获取成功",resultMap));
        }catch (RuntimeException re){
            re.printStackTrace();
            printResult(response,new Result(false,"获取失败"));
        }
    }

    /**
     * 题目答案提交
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @HmRequestMapping("/category/question/commit")
    public void commitQuestion (HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        try {
            // 接收前端数据
            Map<String,Object> mapData = parseJSON2Object(request,HashMap.class);
            // 根据openId，获取会员id
            String openId = getHeaderAuthorization(request);
            WxMember wxMember = wxMemberService.findWxMemberByOpenId(openId);
            mapData.put("memberId",wxMember.getId());
            log.debug("调用Service mapData:{}",mapData);
            // 调用Service，增加做题记录
            categoryService.commitQuestion(mapData);
            // 增加成功
            printResult(response,new Result(true,"题目答案提交成功"));
        }catch (RuntimeException re){
            re.printStackTrace();
            printResult(response,new Result(false,"题目提交失败"+re.getMessage()));
        }
    }
}
